package com.wannengbang.cloudleader.service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.CardListBean;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 信用卡列表
 */
public class CardListAdapter extends RecyclerView.Adapter<CardListAdapter.ViewHolder> {

    private List<CardListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public void setOnCallBackListener(CardListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    public CardListAdapter(List<CardListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_bank_card_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CardListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvBankName.setText(dataBean.getBank_name());
        holder.tvNo.setText(dataBean.getNo());
        holder.tvRealName.setText(dataBean.getReal_name());
        holder.tvDueDay.setText("每月" + dataBean.getDue_day() + "日");
        holder.tvBillDay.setText("每月" + dataBean.getBill_day() + "日");
        String limit = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getLimit()) / 100);
        holder.tvLimit.setText(limit);
        GlideUtils.loadImage(context, holder.ivPicture, dataBean.getBank_image());
        holder.tvDelete.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvEdit.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.itemView.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });
        int num = (position + 1) % 3;
        if (num == 1) {
            holder.llBankBg.setBackgroundResource(R.mipmap.ic_bank_bg1);
        } else if (num == 2) {
            holder.llBankBg.setBackgroundResource(R.mipmap.ic_bank_bg2);
        } else {
            holder.llBankBg.setBackgroundResource(R.mipmap.ic_bank_bg3);
        }

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_picture)
        ImageView ivPicture;
        @BindView(R.id.tv_bank_name)
        TextView tvBankName;
        @BindView(R.id.tv_no)
        TextView tvNo;
        @BindView(R.id.tv_real_name)
        TextView tvRealName;
        @BindView(R.id.tv_due_day)
        TextView tvDueDay;
        @BindView(R.id.tv_bill_day)
        TextView tvBillDay;
        @BindView(R.id.tv_limit)
        TextView tvLimit;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_edit)
        TextView tvEdit;
        @BindView(R.id.ll_bank_bg)
        LinearLayout llBankBg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
